<?php

/**
 * @author Mr Yash
 * @copyright 2007
*/

class Pages_model extends Model {

  
	
	function Pages_model()

    {
        // Call the Model constructor
        parent::Model();
    }
	static function getstatus($status)
	{   
		$out="";
		switch($status)
		{
			case 1: $out="Published"; break;
			case 2: $out="Unpublished"; break;
			case 3: $out="Draft"; break;
			case 4: $out="Private"; break;
			case 5: $out="Trash"; break;
			default: $out="Unknown";
		}
		
		return $out;
	}
	
	function delete_page($post_id,$page_id)
	{
		$page_id=self::RefinePagNav($page_id);
		$this->db->where('PageID', $post_id);
		$query=$this->db->delete('pages');
		redirect('/admin/ManagePages/'.$page_id, 'refresh');
	}

	
	function list_pages($page_id)
	{
		$page_id=self::RefinePagNav($page_id);
		$this->db->from('pages');
	 	$cnt=$this->db->count_all_results();
		
		$config['base_url'] = site_url().'/admin/ManagePages/';
		$config['total_rows'] =$cnt;
		$config['per_page'] = '10';
		$config['num_links'] = '3';
		$config['uri_segment'] = '3';


		$this->pagination->initialize($config); 
		$query = $this->db->get('pages',$config['per_page'],$this->uri->segment(3));
		
		$out= "<table width=\"100%\" border=\"0\">";
		$out.="<caption>Pages List</caption>
			<thead>
				<tr>
					
					<th scope=\"col\">Page Title</th>

					<th scope=\"col\">Status</th>
					<th scope=\"col\">View</th>
					<th scope=\"col\">Edit</th>
					<th scope=\"col\">Delete</th>
				</tr>
			</thead>	
			<tfoot>
				<tr>
					<th scope=\"row\">Total</th>

					<td colspan=\"4\">$cnt Posts</td>
				</tr>
			</tfoot>
			<tbody>";	
		foreach ($query->result() as $row)
		{
			$out.=
			 "<tr>
			     <td>".$row->Title."</td>
			    <td>".self::getstatus($row->Status)."</td>
			    <td>".anchor('blog/page/'.$row->URLTitle, 'View')."</td>
			    <td>".anchor('admin/EditPage/'.$row->PageID.'/'.$page_id,'Edit')."</td>
			    <td>".anchor('admin/DeletePage/'.$row->PageID.'/'.$page_id,'Delete')."</a></td>
			  </tr>";
			
			
		} 
		$out.= "</tbody></table>";
		$out.= $this->pagination->create_links();
		return $out;
		
	}
	
	
	
	function create_page()
	{
	     $insert = array(
                'PageID' => '',
				'Title' => $this->input->post('Title'),
				'URLTitle' => url_title($this->input->post('Title')),
				'Label' => $this->input->post('PageLabel'),
                'Content' => $this->input->post('Content'),
				'Meta' => $this->input->post('Meta'),
				'Keyword' => $this->input->post('Keyword'),
				'Description' => $this->input->post('Description'),
				'Status' => $this->input->post('Status')
                );
           
		    $check=$this->db->insert('pages', $insert);
			
			if($check)
			{
				//Post successfully posted.
				$data['message']="Page is successfully posted.";
				$data['title']="Information Page";
				$data['header']="Information";
				$data['url']='/admin/CreatePage/';
				$data['time']="2";
				$this->load->view('info',$data);
		 
		 	}
	}
	
	function edit_page($blogpage_id,$page_id)
	{
		$page_id=self::RefinePagNav($page_id);
	     $insert = array(
                
				'Title' => $this->input->post('Title'),
				'URLTitle' => url_title($this->input->post('Title')),
				'Label' => $this->input->post('PageLabel'),
                'Content' => $this->input->post('Content'),
				'Meta' => $this->input->post('Meta'),
				'Keyword' => $this->input->post('Keyword'),
				'Description' => $this->input->post('Description'),
				'Status' => $this->input->post('Status')
                );
           $this->db->where(array('PageID'=>$blogpage_id));
		    $check=$this->db->update('pages', $insert);
			
			if($check)
				$data['message']="Page is successfully updated.";
			else
				$data['message']="Page is not successfully updated.";			 
		 		
				$data['title']="Information Page";
				$data['header']="Information";
				$data['url']='/admin/ManagePages/'.$page_id;
				$data['time']="2";
				$this->load->view('info',$data);
	}	
	
	function RefinePagNav($page_id)
	{
			if($page_id==1)
			$page_id=0;
			
			return $page_id;
	}			

}

